package algorithms.sort;

public class QuickSorter {
	public void sort(Integer[] array, int from, int len) {
		quickSort(array, from, len - 1);
	}
	
	private void quickSort(Integer[] array, int from, int to) {
		if(to - from < 1) return;

		int pivot = partion(array, from, to);
		int p = pivot;
		while(p > 0) {
			p = partion(array, from, pivot);
		}
		while(pivot > 0) {
			pivot = partion(array, pivot + 1, to);
		}
	}
	
	private int partion(Integer[] array, int from, int to) {
		int pivot = selectPivot(array,from,to);
		Integer mid = array[pivot];
		array[pivot] = array[to];
		while(from < to) {
			for(; from < to; from ++) {
				if(array[from] > mid) {
					array[to] = array[from];
					break;
				}
			}
			for(; to > from; to --) {
				if(array[to] < mid) {
					array[from] = array[to];
					break;
				}
			}
		}
		array[from] = mid;
		return from;
	}
	private int selectPivot(Integer[] array, int from, int to) {
        return (from+to)/2;
    }
	public static void main(String[] args) {
		Integer[] array = { 47, 23, 58, 16, 24, 10, 75, 34 };
		QuickSorter s = new QuickSorter();
		s.sort(array, 0, array.length);
		for (int i = 0; i < array.length; i++) {
			System.out.print(array[i] + " ");
		}
	}
}
